

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.config.option &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../index.html" />
    <link rel="up" title="pyamp.config" href="../config.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../config.html" accesskey="U">pyamp.config</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.config.option</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The option module contains the Option class for creating a specific</span>
<span class="sd">configuration option, as well as the ClOption class for creating a</span>
<span class="sd">command line option.</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">pyamp.util.dictionary</span> <span class="kn">import</span> <span class="n">addNotNone</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;Option&quot;</span><span class="p">,</span> <span class="s">&quot;ClOption&quot;</span><span class="p">,</span> <span class="s">&quot;ClBoolOption&quot;</span><span class="p">]</span>


<div class="viewcode-block" id="Option"><a class="viewcode-back" href="../../../_generated/pyamp_config_option.html#pyamp.config.option.Option">[docs]</a><span class="k">class</span> <span class="nc">Option</span><span class="p">:</span>
    <span class="sd">&#39;&#39;&#39;The Option class encapsulates a single configuration option. It contains</span>
<span class="sd">    the name of the option, a default value for the option, as well as a</span>
<span class="sd">    function to convert the option into a specific type.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">defaultValue</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">typeFn</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                 <span class="n">commandLine</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * name -- The name of the option</span>
<span class="sd">        * defaultValue -- The default value for this option</span>
<span class="sd">        * typeFn -- The function to convert the value to a specific type</span>
<span class="sd">        * commandLine -- True to be a command line option</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__defaultValue</span> <span class="o">=</span> <span class="n">defaultValue</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__typeFn</span> <span class="o">=</span> <span class="n">typeFn</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__commandLine</span> <span class="o">=</span> <span class="n">commandLine</span>

<div class="viewcode-block" id="Option.isCommandLine"><a class="viewcode-back" href="../../../_generated/pyamp_config_option.html#pyamp.config.option.Option.isCommandLine">[docs]</a>    <span class="k">def</span> <span class="nf">isCommandLine</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Determine if this Option is configurable through the command</span>
<span class="sd">        line.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__commandLine</span>
</div>
<div class="viewcode-block" id="Option.getName"><a class="viewcode-back" href="../../../_generated/pyamp_config_option.html#pyamp.config.option.Option.getName">[docs]</a>    <span class="k">def</span> <span class="nf">getName</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the name of this Option.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__name</span>
</div>
<div class="viewcode-block" id="Option.getDefaultValue"><a class="viewcode-back" href="../../../_generated/pyamp_config_option.html#pyamp.config.option.Option.getDefaultValue">[docs]</a>    <span class="k">def</span> <span class="nf">getDefaultValue</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the default value for this Option.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__defaultValue</span>
</div>
<div class="viewcode-block" id="Option.convert"><a class="viewcode-back" href="../../../_generated/pyamp_config_option.html#pyamp.config.option.Option.convert">[docs]</a>    <span class="k">def</span> <span class="nf">convert</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Convert the given value into the expected type for this Option.</span>

<span class="sd">        * value -- The value to convert</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">convertedValue</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__typeFn</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">convertedValue</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__typeFn</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">convertedValue</span>

</div></div>
<div class="viewcode-block" id="ClOption"><a class="viewcode-back" href="../../../_generated/pyamp_config_option.html#pyamp.config.option.ClOption">[docs]</a><span class="k">class</span> <span class="nc">ClOption</span><span class="p">(</span><span class="n">Option</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The ClOption class encapsulates an :class:`Option` that is configurable</span>
<span class="sd">    through the command line as well as through a configuration file.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">abbr</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">defaultValue</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                 <span class="n">action</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">optionType</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">helpText</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * name -- The name of the option</span>
<span class="sd">        * abbr -- The abbreviated name</span>
<span class="sd">        * defaultValue -- The default value for this option</span>
<span class="sd">        * action -- The action used for this option</span>
<span class="sd">        * optionType -- The type of the option</span>
<span class="sd">        * helpText -- The help text to display</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__abbreviatedName</span> <span class="o">=</span> <span class="n">abbr</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__action</span> <span class="o">=</span> <span class="n">action</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__help</span> <span class="o">=</span> <span class="n">helpText</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__type</span> <span class="o">=</span> <span class="n">optionType</span>

        <span class="n">Option</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">defaultValue</span><span class="o">=</span><span class="n">defaultValue</span><span class="p">,</span>
                        <span class="n">commandLine</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<div class="viewcode-block" id="ClOption.addOption"><a class="viewcode-back" href="../../../_generated/pyamp_config_option.html#pyamp.config.option.ClOption.addOption">[docs]</a>    <span class="k">def</span> <span class="nf">addOption</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Add the command line option to the command line parser.</span>

<span class="sd">        * parser -- The command line parser</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">getName</span><span class="p">()</span>

        <span class="n">keywords</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">addNotNone</span><span class="p">(</span><span class="n">keywords</span><span class="p">,</span> <span class="s">&quot;default&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">getDefaultValue</span><span class="p">())</span>
        <span class="n">addNotNone</span><span class="p">(</span><span class="n">keywords</span><span class="p">,</span> <span class="s">&quot;action&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__action</span><span class="p">)</span>
        <span class="n">addNotNone</span><span class="p">(</span><span class="n">keywords</span><span class="p">,</span> <span class="s">&quot;type&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__type</span><span class="p">)</span>
        <span class="n">addNotNone</span><span class="p">(</span><span class="n">keywords</span><span class="p">,</span> <span class="s">&quot;help&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__help</span><span class="p">)</span>

        <span class="c"># Add the option to the option parser according to the</span>
        <span class="c"># configured options</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__abbreviatedName</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;-</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">__abbreviatedName</span><span class="p">,</span> <span class="s">&quot;--</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">name</span><span class="p">,</span>
                              <span class="o">**</span><span class="n">keywords</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&quot;--</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">keywords</span><span class="p">)</span>

</div></div>
<div class="viewcode-block" id="ClBoolOption"><a class="viewcode-back" href="../../../_generated/pyamp_config_option.html#pyamp.config.option.ClBoolOption">[docs]</a><span class="k">class</span> <span class="nc">ClBoolOption</span><span class="p">(</span><span class="n">ClOption</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The ClBoolOption encapsulates a boolean configuration option.</span>

<span class="sd">    If the boolean does not exist, the option will be set to the default value</span>
<span class="sd">    otherwise it will be set to the inverse of the default value.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">abbr</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">defaultValue</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">helpText</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * name -- The name of the option</span>
<span class="sd">        * abbr -- The abbreviated name for the option</span>
<span class="sd">        * defaultValue -- The default value (True or False)</span>
<span class="sd">        * helpText -- The help text for this option</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">action</span> <span class="o">=</span> <span class="s">&quot;store_</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="ow">not</span> <span class="n">defaultValue</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

        <span class="n">ClOption</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">abbr</span><span class="o">=</span><span class="n">abbr</span><span class="p">,</span> <span class="n">defaultValue</span><span class="o">=</span><span class="n">defaultValue</span><span class="p">,</span>
                          <span class="n">action</span><span class="o">=</span><span class="n">action</span><span class="p">,</span> <span class="n">helpText</span><span class="o">=</span><span class="n">helpText</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../config.html" >pyamp.config</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>